package com;

import java.util.ArrayList;
import java.util.Scanner;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class UI {

	private RulesManager rulesManager = new RulesManager();
	private ViolationsManager violationsManager = new ViolationsManager();
	
	public String displayRules(boolean isAdmin){
		String ret = "";
		
		ArrayList<Rule> rules = rulesManager.queryRules(-1);
		int index = 1;
		
		if (isAdmin){
			for (Rule r : rules){
				ret += "<form method=\"post\" action=\"RulesServlet\">" +
						"<TR>" + 
						"<TD><input type=\"hidden\" name=\"id\" value=\"" + r.getID() + "\">" + index++ + "</TD>" +
						"<TD>" + r.getRuleName() + "</TD>" +
						"<TD>" + r.getDataField() + "</TD>" +
						"<TD>" + r.getSpecification() + "</TD>" +
						"<TD>" + r.getBoundaryType() + "</TD>" +
						"<TD>" + r.getBoundaryCondition() + "</TD>" +
						"<TD><button type=\"submit\" class=\"btn btn-default\" name=\"button\" value=\"edit\">Edit</button></TD>" + 
						"<TD><button type=\"submit\" class=\"btn btn-default\" name=\"button\" value=\"delete\">Delete</button></TD></TR>" +
						"</form>";
			}
		}else{
			for (Rule r : rules){
				ret += "<form method=\"post\" action=\"RulesServlet\">" +
						"<TR>" + 
						"<TD><input type=\"hidden\" name=\"id\" value=\"" + r.getID() + "\">" + index++ + "</TD>" +
						"<TD>" + r.getRuleName() + "</TD>" +
						"<TD>" + r.getDataField() + "</TD>" +
						"<TD>" + r.getSpecification() + "</TD>" +
						"<TD>" + r.getBoundaryType() + "</TD>" +
						"<TD>" + r.getBoundaryCondition() + "</TD>" +
						"</form>";
			}
		}
		return ret;
	}
	
	public String displayRulesDropdown(HttpServletRequest request){
		String ret = "";
		
		String selected = request.getParameter("ruleFilter");
		
		ArrayList<Rule> rules = rulesManager.queryRules(-1);
		for (Rule r : rules){
			ret += "<option";
			if (r.getRuleName().equals(selected)){
				ret += " selected=\"true\"";
			}
			ret += ">" + r.getRuleName() + "</option>";
		}
		
		return ret;
	}
	
	public String displayRuleNameInput(HttpServletRequest request){
		String ret = "";
		try{
			Rule rule = (Rule) request.getAttribute("rule");
			String ruleName = (rule == null) ? "" : rule.getRuleName();			
			
			ret = "<input type=\"text\" class=\"form-control\" id=\"inputRuleName\" " +
					"name=\"ruleName\" placeHolder=\"Enter a rule name\" value=\"" + ruleName + "\">";

		}catch(Exception e){
			e.printStackTrace();
		}
		
		return ret;
	}
	
	public String displayDataFieldsDropdown(HttpSession session, HttpServletRequest request){
		String ret = "";
		try{
			Rule rule = (Rule) request.getAttribute("rule");
			String dataField = (rule == null) ? "" : rule.getDataField();
			
			Scanner reader = new Scanner(session.getServletContext().getResourceAsStream("dataFields.txt"));
			while (reader.hasNext()){
				String nextLine = reader.nextLine();
				ret += "<option";
				if (dataField.equals(nextLine)){
					ret += " selected=\"true\"";
				}
				ret += ">" + nextLine + "</option>";
			}
			reader.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return ret;
	}
	
	public String displaySpecificationsDropdown(HttpSession session, HttpServletRequest request){
		String ret = "";
		try{
			Rule rule = (Rule) request.getAttribute("rule");
			String specification = (rule == null) ? "" : rule.getSpecification();
			
			Scanner reader = new Scanner(session.getServletContext().getResourceAsStream("specifications.txt"));
			while (reader.hasNext()){
				String nextLine = reader.nextLine();
				ret += "<option";
				if (specification.equals(nextLine)){
					ret += " selected=\"true\"";
				}
				ret += ">" + nextLine + "</option>";
			}
			reader.close();
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return ret;
	}
	
	public String displayBoundaryTypesDropdown(HttpSession session, HttpServletRequest request){
		String ret = "";
		try{
			Rule rule = (Rule) request.getAttribute("rule");
			String boundaryType = (rule == null) ? "" : rule.getBoundaryType();
			
			Scanner reader = new Scanner(session.getServletContext().getResourceAsStream("boundaryTypes.txt"));
			while (reader.hasNext()){
				String nextLine = reader.nextLine();
				ret += "<option";
				if (boundaryType.equals(nextLine)){
					ret += " selected=\"true\"";
				}
				ret += ">" + nextLine + "</option>";
			}
			reader.close();
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return ret;
	}
	
	public String displayBoundaryConditionInput(HttpServletRequest request){
		String ret = "";
		try{
			Rule rule = (Rule) request.getAttribute("rule");
			String boundaryCondition = (rule == null) ? "" : rule.getBoundaryCondition();
			
			ret = "<input type=\"text\" class=\"form-control\" id=\"inputBoundaryCondition\" " +
					"name=\"boundaryCondition\" placeHolder=\"Enter a value\" value=\"" + boundaryCondition + "\">";

		}catch(Exception e){
			e.printStackTrace();
		}
		
		return ret;
	}
	
	public String displayRuleViolations(HttpServletRequest request){
		String ret = "";
		
		ArrayList<Violation> violations = violationsManager.queryViolations(request.getParameter("ruleFilter"));
		for (Violation v : violations){
			ret += "<TR>" +
					"<TD>" + v.getRuleText() + "</TD>" +
					"<TD>" + v.getCity() + "</TD>" +
					"<TD>" + v.getYear() + "</TD>" +
					"<TD>" + v.getDataField() + "</TD></TR>";
		}
		
		return ret;
	}
}
